package com.nanxhs.structure.tree;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 中(根)序遍历迭代器
 * @author 唐海斌
 * @version 1.0
 * @date 2021/5/25 10:00
 */
public class MiddleIterator {

    private List<Integer> dataList;

    private final Tree.TreeNode root;

    public MiddleIterator(Tree.TreeNode root) {
        this.root = root;
    }

    /**
     * 中(根)序遍历
     *
     * 左-根-右
     */
    public List<Integer> iterator() {
        dataList = new ArrayList<>(10);
        iterator(root);
        return dataList;
    }

    private void iterator(Tree.TreeNode parentNode) {
        if (parentNode == null) {
            return;
        }
        if (parentNode.getChildrenLeft() != null) {
            iterator(parentNode.getChildrenLeft());
        }
        dataList.add(parentNode.getElement());
        if (parentNode.getChildrenRight() != null) {
            iterator(parentNode.getChildrenRight());
        }
    }
}
